const app = getApp()
const db = wx.cloud.database();
const config = require("../../config.js");
const _ = db.command;
Page({

      /**
       * 页面的初始数据
       */
      data: {
            num: 5,
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {

            this.getuser()
      },
      //金额输入，因为js对小数乘除很容易出问题，所以干脆就取整
      numInput(e) {
            this.data.num = e.detail.value
      },
      // 获取用户
      getuser() {
            let that = this;
            db.collection('user').where({
                  _openid: app.openid
            }).get({
                  success: function (res) {
                        console.log(res.data)
                        let info = res.data[0];
                        that.setData({
                              userPolishNum: info.userPolishNum,
                              parse: info.parse,
                              _id: info._id,
                              PolishTime: info.PolishTime
                        })
                  },
                  fail() {
                        wx.showToast({
                              title: '获取失败',
                              icon: 'none'
                        })
                  }
            })
      },
      //校检
      check(e) {
            let that = this;
            let currentTime = new Date().getTime()
            //每日仅限次数一次
            if (currentTime - that.data.PolishTime < 86400000) {
                  wx.showToast({
                        title: '每日仅限次数一次，请明日再来',
                        icon: 'none',
                  })
                  return false;
            }

            //校检次数不得为空
            if (!that.data.num) {
                  wx.showToast({
                        title: '请输入充值次数',
                        icon: 'none',
                  })
                  return false;
            }

            let num = parseInt(that.data.num);
            //校检余额是否可以支付
            if (that.data.parse < num) {
                  wx.showToast({
                        title: '余额不足以抵扣次数金额',
                        icon: 'none',
                  })
                  return false;
            }
            //校检次数不得高于5
            if (num > 5) {
                  wx.showToast({
                        title: '每次最多充值5次',
                        icon: 'none',
                  })
                  return false;
            }
            that.paypost();
      },
      //提交
      paypost() {
            let that = this;
            let num = parseInt(that.data.num);
            let userPolishNum = parseInt(that.data.userPolishNum);
            let parse = parseInt(that.data.parse)
            if (!num > 0) {
                  wx.showToast({
                        title: '请输入次数',
                        icon: 'none'
                  })
                  return false;
            }
            wx.showLoading({
                  title: '正在充值',
            });
            db.collection('user').doc(that.data._id).update({
                  // data 传入需要局部更新的数据
                  data: {
                        userPolishNum: userPolishNum + num,
                        parse: parse - num,
                        PolishTime: new Date().getTime()
                  },
                  success: function (res) {
                        console.log(res.data)
                        that.up(num)
                        wx.showToast({
                              title: '充值成功',
                              icon: 'success',
                              success: function () {
                                    setTimeout(function () {
                                          wx.redirectTo({
                                                url: '/pages/parse/parse',
                                          })
                                    }, 1000)
                              }
                        });

                  }
            })


      },
      //次数计算
      up(num) {
            let that = this;
            wx.cloud.callFunction({
                  name: 'his',
                  data: {
                        $url: "Increase", //云函数路由参数
                        num: num
                  },
                  success(e) {
                        wx.showToast({
                              title: '充值成功',
                              icon: 'success',
                              duration: 1000,
                              success: function () {
                                    setTimeout(function () {
                                          wx.navigateBack({
                                                url: '/pages/parse/parse',
                                          })
                                    }, 1000)
                              }
                        })

                        that.history('擦亮次数', num, 1);
                        that.history('充值次数扣款', num, 2);

                  },
                  fail(e) {
                        wx.showToast({
                              title: '发送错误，请联系管理员',
                              icon: 'none'
                        })
                  }
            })
      },

      //历史记录
      history(name, num, type) {
            let that = this;
            db.collection('history').add({
                  data: {
                        stamp: new Date().getTime(),
                        type: type, //1充值2支付
                        name: name,
                        num: num,
                        oid: app.openid,
                  },
                  success: function (res) {
                        console.log(res)
                  },
                  fail: console.error
            })
      },

})